/******************************************************************************/
//	Actor.cpp	-	A basic character
//
//	Author:		Russell Klinkerman
//	Created:	01.18.2004
/******************************************************************************/

#include "Actor.h"
#include "FSMScript.h"

#include "EventFactory.h"

void	CActor::Init()
{
	// Load graphics
	dxiActor.Load("bin\\anim_chunli.bmp", D3DCOLOR_XRGB(255,0,255));

	// Set Animations
	animIdle.Set	(	&dxiActor,	// The source image for the animation
						4,			// number of rows in source image
						8,			// number of columns in source image
						0,			// Starting index for this animation
						18,			// Number of frames in this animation
						100,		// Animation's frequency for changing frames
						true		// Loop Animation? (Default - true)
					);

	animFight.Set(&dxiActor, 4, 8, 18, 5, 100);
	animBlock.Set(&dxiActor, 4, 8, 23, 9, 100);

	pActiveAnimation = &animIdle;	// Starting animation

	// Set actor matrix
	D3DXMatrixIdentity(&actorMatrix);
	D3DXMatrixTranslation(&actorMatrix, 375.0f, 185.0f, 0.0f);

	// Load chart files
	dxiFlowBackground.Load("bin\\flow.tga", D3DCOLOR_XRGB(255,0,255));
	dxiFlowMarker.Load("bin\\flow_marker.tga", D3DCOLOR_XRGB(255,0,255));

	// Chart Setup
	dxcFlow.Set(&dxiFlowBackground, &dxiFlowMarker);

	dxcFlow.AddAnchor(CHART_IDLE, 127.0f,49.0f);	// Add anchor for idle circle
	dxcFlow.AddAnchor(CHART_BLOCK, 208.0f,189.0f);	// Add anchor for block circle
	dxcFlow.AddAnchor(CHART_FIGHT, 48.0f,189.0f);	// Add anchor for fight circle

	dxcFlow.SetAnchor(CHART_IDLE);	// Set idle circle active

	D3DXMatrixIdentity(&mFlow);
	D3DXMatrixTranslation(&mFlow, 40.0f, 135.0f, 0.0f);	// Set chart's screen position.

	// Initialize the State Machine
	stateMachine.Initialize(this);

	// Load the ChunLi Script File into the FSM
	LoadScript("fsmScript.txt");
}

bool	CActor::Update()
{
	// Maintain the message queue
	while(messageQueue.size() > 5)
	{
		// Pop the oldest message
		messageQueue.pop_back();
	}

	stateMachine.Update();

	return true;
}

void	CActor::LoadScript(char * _szScriptFile)
{
	// Clear existing transition table
	stateMachine.ClearTransitionTable();

	FSMScript script;

	/* The Load(---) function uses a script to create instances of states and then populate the
		transition table.  The transition table is filled in like this:

			table[ a state id ][ the input id ] = next state pointer;
	*/
	script.Load(_szScriptFile, &stateMachine);	// "test.fsmScript.txt"
}

void	CActor::Draw()
{
	pActiveAnimation->Draw(&actorMatrix, true);
	dxcFlow.Draw(&mFlow, true);

	int i = 0;
	list< string >::iterator	listIter;
	for(listIter = messageQueue.begin(); listIter != messageQueue.end(); listIter++, i++)
	{
		d3dManager.Text( (char*)(*listIter).c_str(), 400, 80 - (15 * i), 255 - (40 * i), 255 - (40 * i), 0 );
	}
}

void	CActor::Shutdown()
{
	stateMachine.Shutdown();

	if(CState::nStatesDeleted != CState::nStatesNewed)
		MessageBox(NULL, "States still in memory!", "ERROR! Memory Leak", MB_ICONWARNING);

	dxiActor.Unload();
}